package cn.jbolt.core.service;

import cn.jbolt.core.base.JBoltMsg;
import cn.jbolt.core.bean.Option;
import cn.jbolt.core.common.enums.JBoltSystemLogTargetType;
import cn.jbolt.core.db.sql.Sql;
import cn.jbolt.core.kit.JBoltUserKit;
import cn.jbolt.core.model.Post;
import cn.jbolt.core.service.base.JBoltBaseService;
import com.jfinal.kit.Okv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;
/**
 *
 * @ClassName: PostService
 * @author: JFinal学院-小木
 * @date: 2021-02-11 12:12
 */
public class JBoltPostService extends JBoltBaseService<Post> {
	private final Post dao = new Post().dao();
	@Override
	protected Post dao() {
		return dao;
	}

  /**
	 *分页 获取数据
	 * @param pageNumber
	 * @param pageSize
	 * @param type
	 * @param enable
	 * @param keywords
	 * @return
	 */
	public Page<Post> paginateAdminDatas(Integer pageNumber, Integer pageSize,Integer type,Boolean enable,String keywords) {
		Sql sql = selectSql().page(pageNumber,pageSize);
		sql.eq(TYPE,type);
		sql.enableEq(enable);
		sql.likeMulti(keywords,NAME,SN,REMARK);
		sql.orderBySortRank();
		return paginate(sql);
	}

	/**
	 * 获取可用list
	 * @return
	 */
	public List<Post> getEnableList() {
		return getCommonList(Okv.by(ENABLE, TRUE),SORT_RANK,ASC);
	}

  /**
	 * 保存
	 * @param post
	 * @return
	 */
	public Ret save(Post post) {
		if(post==null||isOk(post.getId())||notOk(post.getName())||notOk(post.getSn())||notOk(post.getType())) {
			return fail(JBoltMsg.PARAM_ERROR);
		}
		if(existsName(post.getName())) {return fail(JBoltMsg.DATA_SAME_NAME_EXIST);}
		if(exists(SN,post.getSn())) {return fail(JBoltMsg.DATA_SAME_SN_EXIST);}
		post.setSortRank(getNextSortRank());
		post.setEnable(true);
		boolean success=post.save();
		if(success) {
			//添加日志
			addSaveSystemLog(post.getId(), JBoltUserKit.getUserId(),post.getName());
		}
		return ret(success);
	}

  /**
	 * 保存
	 * @param post
	 * @return
	 */
	public Ret update(Post post) {
		if(post==null||notOk(post.getId())||notOk(post.getName())||notOk(post.getSn())||notOk(post.getType())) {
			return fail(JBoltMsg.PARAM_ERROR);
		}
		Post dbPost = findById(post.getId());
		if(dbPost==null) {return fail(JBoltMsg.DATA_NOT_EXIST);}
		if(existsName(post.getName(), post.getId())) {return fail(JBoltMsg.DATA_SAME_NAME_EXIST);}
		if(exists(SN,post.getSn(), post.getId())) {return fail(JBoltMsg.DATA_SAME_SN_EXIST);}
		boolean success=post.update();
		if(success) {
			//添加日志
			addUpdateSystemLog(post.getId(), JBoltUserKit.getUserId(),post.getName());
		}
		return ret(success);
	}

   /**
	  * 删除
	 * @param id
	 * @return
	 */
	public Ret delete(Long id) {
		Ret ret=deleteById(id,false);
		if(ret.isOk()){
			//添加日志
			Post post=ret.getAs(KEY_DATA);
			addDeleteSystemLog(id, JBoltUserKit.getUserId(), post.getName());
		}
		return ret;
	}

	/**@Override
	public String checkInUse(Post post) {
		//这里用来覆盖 检测Post是否被其它表引用
		return null;
	}*/

   /**
	  * 切换禁用启用状态
	 * @param id
	 * @return
	 */
	public Ret toggleEnable(Long id) {
		Ret ret=toggleBoolean(id, ENABLE);
		if(ret.isOk()){
			//添加日志
			Post post=ret.getAs(KEY_DATA);
			addUpdateSystemLog(id, JBoltUserKit.getUserId(), post.getName(),"的启用状态:"+post.getEnable());
		}
		return ret;
	}

   /**
	  * 上移
	 * @param id
	 * @return
	 */
	public Ret up(Long id) {
		Post post=findById(id);
		if(post==null){
			return fail("数据不存在或已被删除");
		}
		Integer rank=post.getSortRank();
		if(rank==null||rank<=0){
			return fail("顺序需要初始化");
		}
		if(rank==1){
			return fail("已经是第一个");
		}
		Post upPost=findFirst(Okv.by(SORT_RANK, rank-1));
		if(upPost==null){
			return fail("顺序需要初始化");
		}
		upPost.setSortRank(rank);
		post.setSortRank(rank-1);

		upPost.update();
		post.update();
		return SUCCESS;
	}

/**
	 * 下移
	 * @param id
	 * @return
	 */
	public Ret down(Long id) {
		Post post=findById(id);
		if(post==null){
			return fail("数据不存在或已被删除");
		}
		Integer rank=post.getSortRank();
		if(rank==null||rank<=0){
			return fail("顺序需要初始化");
		}
		int max=getCount();
		if(rank==max){
			return fail("已经是最后一个");
		}
		Post upPost=findFirst(Okv.by(SORT_RANK, rank+1));
		if(upPost==null){
			return fail("顺序需要初始化");
		}
		upPost.setSortRank(rank);
		post.setSortRank(rank+1);

		upPost.update();
		post.update();
		return SUCCESS;
	}

   /**
	  * 初始化排序
	 */
	public Ret initRank(){
		List<Post> allList=findAll();
		if(allList.size()>0){
			for(int i=0;i<allList.size();i++){
				allList.get(i).setSortRank(i+1);
			}
			batchUpdate(allList);
		}
		//添加日志
		addUpdateSystemLog(null, JBoltUserKit.getUserId(),"所有数据", "的顺序:初始化所有");
		return SUCCESS;
	}


	public List<Option> getEnableOptionsList() {
		return getOptionList(NAME, ID, Okv.by(ENABLE, TRUE), SORT_RANK, ASC);
	}

	@Override
	protected int systemLogTargetType() {
		return JBoltSystemLogTargetType.POST.getValue();
	}

}
